package cn.tedu._06article.base.filter;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
@Slf4j
@WebFilter(filterName = "logcheck", urlPatterns = "/*")
public class logcheck implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        log.debug("logcheck.init");
    }

    @Override
    public void destroy() {
        log.debug("logcheck.destroy");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
log.debug("logcheck.doFilter");
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        HttpSession session = request.getSession();
        if(session.getAttribute("user") != null)
        {filterChain.doFilter(request,response);}
        else{ response.sendRedirect("v1/user/login"); }

     }

}
